<template>
  <div class="login">
    <!-- 用于显示名字 -->
    <div class="header">
      <p class="title"></p>
    </div>

    <!-- 内容 -->
    <div class="content">
      <!-- 放图片 -->
      <div class="content-left">
        <img src="@/assets/images/login-background.jpg" class="img-bg" />
      </div>
      <!-- 放表单 -->
      <div class="content-right">
        <div class="content-right-title">服装店销售管理系统</div>
        <el-form
          ref="loginForm"
          :model="loginForm"
          :rules="loginRules"
          class="login-form"
        >
          <el-form-item prop="username">
            <el-input
              v-model="loginForm.username"
              type="text"
              auto-complete="off"
              placeholder="账号"
            >
              <svg-icon
                slot="prefix"
                icon-class="user"
                class="el-input__icon input-icon"
              />
            </el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              v-model="loginForm.password"
              type="password"
              auto-complete="off"
              placeholder="密码"
              @keyup.enter.native="handleLogin"
            >
              <svg-icon
                slot="prefix"
                icon-class="password"
                class="el-input__icon input-icon"
              />
            </el-input>
          </el-form-item>
          <el-checkbox
            v-model="loginForm.rememberMe"
            style="margin: 0px 0px 25px 0px"
            >记住密码</el-checkbox
          >
          <el-form-item style="width: 100%">
            <el-button
              :loading="loading"
              size="medium"
              type="primary"
              style="width: 100%"
              @click.native.prevent="handleLogin"
            >
              <span v-if="!loading">登 录</span>
              <span v-else>登 录 中...</span>
            </el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>
<script>
import Cookies from "js-cookie";
import { encrypt, decrypt } from "@/utils/jsencrypt";

export default {
  name: "Login",
  data() {
    return {
      codeUrl: "",
      loginForm: {
        username: "",
        password: "",
        rememberMe: false,
        code: "",
        uuid: "",
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入您的账号" },
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" },
        ],
        code: [{ required: true, trigger: "change", message: "请输入验证码" }],
      },
      loading: false,
      redirect: undefined,
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true,
    },
  },
  created() {
    this.getCookie();
  },
  methods: {
    getCookie() {
      const username = Cookies.get("username");
      const password = Cookies.get("password");
      const rememberMe = Cookies.get("rememberMe");
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password:
          password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe),
      };
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          if (this.loginForm.rememberMe) {
            Cookies.set("username", this.loginForm.username, { expires: 30 });
            Cookies.set("password", encrypt(this.loginForm.password), {
              expires: 30,
            });
            Cookies.set("rememberMe", this.loginForm.rememberMe, {
              expires: 30,
            });
          } else {
            Cookies.remove("username");
            Cookies.remove("password");
            Cookies.remove("rememberMe");
          }
          this.$store
            .dispatch("Login", this.loginForm)
            .then(() => {
              this.$store.dispatch("tagsView/delAllViews");
              this.$router.push({ path: this.redirect || "/" }).catch(() => {});
            })
            .catch(() => {
              this.loading = false;
            });
        }
      });
    },
  },
};
</script>
<style scoped rel="stylesheet/scss" lang="scss">
.container {
  width: 1080px;
  margin: 0 auto;
  position: relative;
}
/* 通栏 */
.container-fluid {
  width: 100%;
}

/* 页面配置 */
.login {
  height: 100%;
  background: #92b6d5;
}

.header {
  width: 100%;
  height: 160px;
  display: flex;
  justify-content: center;
}
.title {
  font-size: 33px;
  color: rgba(0, 0, 0, 0.85);
  font-family: Chinese Quote, -apple-system, BlinkMacSystemFont, Segoe UI,
  PingFang SC, Hiragino Sans GB, Microsoft YaHei, Helvetica Neue, Helvetica,
  Arial, sans-serif, Apple Color Emoji, Segoe UI Emoji, Segoe UI Symbol;
  font-weight: 600;
  position: relative;
  top: 75px;
}

/* 内容配置 */
.content {
  margin: 0 auto;
  width: 1000px;
  height: 620px;
  background: #f0f2f5;
  border-radius: 40px;
  display: flex;
  justify-content: space-around;
  align-items: center;
}
.img-bg {
  height: 100%;
  width: 660px;
  border-radius: 40px;
}
.content-left {
  height: 100%;
  display: flex;
  align-items: center;
}
.content-right {
  width: 400px;
  height: 100%;
  background: #ffffff;
  border-radius: 40px;
  margin-left: -60px;
  padding: 30px;
}
.content-right-title {
  margin-top: 100px;
  text-align: center;
  font-size: 20px;
  font-weight: bold;
  padding-bottom: 10px;
}

.login-form {
  border-radius: 6px;
  background: #ffffff;
  width: 350px;
  // padding: 25px 25px 5px 25px;
  margin-left: 0px;
  .el-input {
    height: 38px;
    input {
      height: 38px;
    }
  }
  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}
.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}
.login-code {
  width: 33%;
  height: 38px;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}
.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}
.login-code-img {
  height: 38px;
}
.el-blog {
  text-align: center;
  color: #ffffff;
  margin-top: 5px;
  background-color: #97C5F5;
  border-radius: 4px;
}
</style>
